package com.igeek.collateral.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.entity.Inbound;
import com.igeek.collateral.entity.Outbound;
import com.igeek.collateral.mapper.InboundMapper;
import com.igeek.collateral.mapper.OutboundMapper;
import com.igeek.collateral.service.IInboundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.collateral.vo.InOutNumberVo;
import com.igeek.collateral.vo.PageQueryVO;
import com.igeek.collateral.vo.ZInboundVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;

/**
 * <p>
 * 入库记录 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-04
 */
@Service
public class InboundServiceImpl extends ServiceImpl<InboundMapper, Inbound> implements IInboundService {


    @Autowired
    private InboundMapper inboundMapper;

    @Transactional(readOnly = true)
    @Override
    public Page<Inbound> findPageIn(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        //封装Page对象
        Page<Inbound> page = new Page<>(currentPage , pageSize);
        //封装查询条件(根据入库编号查询)
        LambdaQueryWrapper<Inbound> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Inbound::getResult,1)
                .eq(Inbound::getStatus,1)
                .like(StringUtils.hasLength(queryString),Inbound::getNum,queryString);
        //执行分页查询条件
        Page<Inbound> pageIn = this.baseMapper.selectPage(page,lambdaQueryWrapper);
        return pageIn;
    }


    //根据id删除
    @Transactional
    @Override
    public boolean delete(Long id) {
        LambdaQueryWrapper<Inbound> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Inbound::getId,id);
        int i=this.baseMapper.delete(lambdaQueryWrapper);
        return i>0?true:false;
    }

    @Override
    public Page<Inbound> findPageAll(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        //封装Page对象
        Page<Inbound> page = new Page<>(currentPage , pageSize);
        //封装查询条件(根据入库编号查询)
        LambdaQueryWrapper<Inbound> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Inbound::getStatus,1)
                .like(StringUtils.hasLength(queryString),Inbound::getNum,queryString);
        //执行分页查询条件
        Page<Inbound> pageIn = this.baseMapper.selectPage(page,lambdaQueryWrapper);
        return pageIn;
    }

    @Override
    public boolean updateRS(Inbound inbound, Integer collateralId) {
        //先删除表中关系
        inboundMapper.delete(new LambdaQueryWrapper<Inbound>().eq(Inbound::getId , inbound.getId()));
        //在插入关系
        this.addConnectS(inbound,collateralId);
        //最后执行更新操作
        int b = inboundMapper.updateById(inbound);
        return b>0?true:false;
    }

    @Override
    public boolean updatePart(ZInboundVo vo,Integer inboundId) {
        Inbound inbound = inboundMapper.selectById(inboundId);
        //点击确定好后，直接修改其入库状态(改为已入库),同时将结果改为通过
        inbound.setStatus(1);
        inbound.setResult(1);
        inbound.setDate(vo.getDate());
        inbound.setDescription(vo.getDescription());
        inbound.setAuditStaff(vo.getAuditStaff());
        inbound.setApplyStaff(vo.getApplyStaff());
        int i = inboundMapper.updateById(inbound);
        return i>0?true:false;
    }


    //找到匹配的进库信息，并判断其类型(只能保证一条进库信息只能放一个押品信息)
    @Override
    public int check(Integer collateralId) {
        LambdaQueryWrapper<Inbound>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Inbound> eq = lambdaQueryWrapper.eq(Inbound::getCollateralId, collateralId);
        Inbound inbound = this.baseMapper.selectOne(eq);
        int type = 0;
        //将类型返回的同时，证明权证管理员点击了确定信息，就需要将这条进库信息的状态修改为已进库
        inbound.setStatus(1);
        type= inbound.getType();
        return type;
    }



    //根据押品Id删除对应的进库信息
    @Override
    public boolean deleteMessByCid(Integer collId) {
        LambdaQueryWrapper<Inbound>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Inbound> eq = lambdaQueryWrapper.eq(Inbound::getCollateralId, collId);
        int b = this.baseMapper.delete(eq);
        return b>0?true:false;
    }


    @Override
    public boolean update(Inbound io, Integer collateralId) {
        //先删除表中关系
        inboundMapper.delete(new LambdaQueryWrapper<Inbound>().eq(Inbound::getId , io.getId()));
        //在插入关系
        this.addConnect(io,collateralId);
        //最后执行更新操作
        int b = inboundMapper.updateById(io);
        return b>0?true:false;
    }


    //添加进库信息
    @Override
    public boolean add(Inbound inbound, Integer collateralId) {
        //正常进库
        if(inbound.getType()==0) {
            inbound.setNum(UUID.randomUUID().toString());
            inbound.setStatus(0);
            inbound.setResult(0);
            Inbound io = inbound.setCollateralId(collateralId);
            int a = inboundMapper.insert(io);
            return a>0?true:false;
        }
        else if(inbound.getType()==1)       //置换进库
        {
            inbound.setNum(UUID.randomUUID().toString());
            inbound.setStatus(1);
            Inbound io = inbound.setCollateralId(collateralId);
            int b = inboundMapper.insert(io);
            return b>0?true:false;
        }
        return false;
    }


    @Transactional(readOnly = true)
    @Override
    public Page<Inbound> findPageZ(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        //封装Page对象
        Page<Inbound> page = new Page<>(currentPage , pageSize);
        //封装查询条件(根据入库编号查询)
        LambdaQueryWrapper<Inbound> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Inbound::getStatus,1)
                .eq(Inbound::getType,0)
                .like(StringUtils.hasLength(queryString),Inbound::getNum,queryString);
        //执行分页查询条件
        Page<Inbound> pageIZ = this.baseMapper.selectPage(page,lambdaQueryWrapper);
        return pageIZ;
    }



    //置换入库分页查询
    @Transactional(readOnly = true)
    @Override
    public Page<Inbound> findPageR(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        //封装Page对象
        Page<Inbound> page = new Page<>(currentPage , pageSize);
        //封装查询条件(根据入库编号查询)
        LambdaQueryWrapper<Inbound> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Inbound::getStatus,1)
                .eq(Inbound::getType,1)
                .like(StringUtils.hasLength(queryString),Inbound::getNum,queryString);
        //执行分页查询条件
        Page<Inbound> pageIR = this.baseMapper.selectPage(page,lambdaQueryWrapper);
        return pageIR;
    }


    //给进库表中插入信息()
    public void addConnect(Inbound io, Integer collateralId) {
        //Collateral collateral = new Collateral();
        Inbound inbound = io.setCollateralId(collateralId);
        inboundMapper.insert(inbound);
    }





    //点击同意后的
    // inbound.setStatus(1);
    // inbound.setResult(1);
    public void addConnectS(Inbound io, Integer collateralId) {
        //Collateral collateral = new Collateral();
        Inbound inbound = io.setCollateralId(collateralId);
        //置换操作改变状态和结果
        inbound.setStatus(1);
        inbound.setResult(1);
        inboundMapper.insert(inbound);
    }

    //修改进库对应的押品信息(一对一)
    //update inbound i,collateral c set c.name='gggggggggg',c.address='安徽省宣城市郎溪县',c.code='UUUUUfrr',i.date='2000-09-24' where i.collateral_id=c.id and i.id=2

}
